package net.pulseproject.router.app.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.pulseproject.commons.midi.collection.InOutDevices;
import net.pulseproject.commons.midi.entity.Device;
import net.pulseproject.commons.midi.entity.DeviceComparator;
import net.pulseproject.commons.misc.binder.Binder;
import net.pulseproject.router.app.model.Model;
import net.pulseproject.router.app.model.ModelField;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PortsWindow extends JDialog {

	private static final long serialVersionUID = 3921063116921815056L;

	private static final Log LOG = LogFactory.getLog(PortsWindow.class);
	
	private final JPanel transmittersPanel = new JPanel();
	
	private final JPanel receiversPanel = new JPanel();
	
	private final Font headerFont = new Font(Font.DIALOG, Font.BOLD, 12);
	
	public PortsWindow(final Model model) {
		this.setTitle("Available Ports");

		model.addBinding(new Binder<ModelField, InOutDevices>() {
			@Override
			public ModelField getField() {
				return ModelField.AVAILABLE_DEVICES;
			}
			public void onValueSet(final Object value) {
				if(value == null) {
					PortsWindow.this.onAvailableDevicesSet(null);
				} else {
					PortsWindow.this.onAvailableDevicesSet((InOutDevices) value);
				}
			}
		});
		
		this.setResizable(false);
		this.getContentPane().add(this.initComponents());
		this.pack();
	}
	
	private void onAvailableDevicesSet(final InOutDevices devices) {
		LOG.debug("onAvailableDevicesSet(devices=" + devices + ")");
		
		this.reinitPanel(this.transmittersPanel, (devices == null ? null : devices.getTransmitters()), "Transmitters");
		this.reinitPanel(this.receiversPanel, (devices == null ? null : devices.getReceivers()), "Receivers");
		this.pack();
	}
	
	private void reinitPanel(final JPanel panel, final Collection<Device> devices, final String label) {
		panel.removeAll();
		
		final JLabel header = new JLabel(label);
		header.setFont(this.headerFont);
		panel.add(header);
		
		if(devices == null) {
			panel.add(new JLabel("N/A"));
			return;
		}
		
		final List<Device> sortedDevices = new LinkedList<Device>(devices);
		Collections.sort(sortedDevices, new DeviceComparator());
		for (final Device device : sortedDevices) {
			panel.add(new JLabel(device.getPortName()));
		}
	}

	private Component initComponents() {
		final JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(30, 10));
		panel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
		
		panel.add(new JLabel("This is a list of all available MIDI ports:"), BorderLayout.NORTH);
		this.transmittersPanel.setLayout(new BoxLayout(this.transmittersPanel, BoxLayout.Y_AXIS));
		panel.add(this.transmittersPanel, BorderLayout.WEST);
		this.receiversPanel.setLayout(new BoxLayout(this.receiversPanel, BoxLayout.Y_AXIS));
		panel.add(this.receiversPanel, BorderLayout.EAST);
		
		return panel;
	}
	
}
